package com.xuxin.baby.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.ChildSicknessAdapter;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.ChildSicknessBean;
import com.xuxin.baby.contract.ChildSicknessContract;
import com.xuxin.baby.presenter.ChildSicknessPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.BaseUtils;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.18 0018
 * 看病记录
 */
public class ChildSicknessActivity extends BaseActivity implements ChildSicknessContract.View {
    private final ChildSicknessContract.Presenter presenter = new ChildSicknessPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;

    private ChildSicknessAdapter myAdapter;
    private TextView child_sickness_count;
    private final String[] sickness_count = new String[]{"所有记录", "最近10次", "最近20次", "最近30次", "最近40次", "最近50次"};
    private String tenantId;//租户id
    private String token;//授权token
    private String childId;//宝宝ID
    private int pageNum = 0;
    private int maxCount = 0;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_smart);
    }

    @Override
    public void initView() {
        title_name.setText("记看病");
        smart_refresh.setBackgroundColor(Color.WHITE);
        smart_refresh.setEnableLoadMore(false);
        smart_refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onRefresh");
                pageNum = 0;
                myAdapter.setList(null);
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LogUtils.e("onLoadMore");
                handler.sendEmptyMessage(0);
            }
        });
        View header = View.inflate(context, R.layout.item_child_sickness_header, null);
        header.findViewById(R.id.child_sickness_record).setOnClickListener(this);
        child_sickness_count = header.findViewById(R.id.child_sickness_count);
        child_sickness_count.setOnClickListener(this);
        RecycleUtils.init(smart_recycle);
        myAdapter = new ChildSicknessAdapter();
        smart_recycle.setAdapter(myAdapter);
        myAdapter.addHeaderView(header);
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putBoolean("detail", true);
                bundle.putString("medicalRecord", myAdapter.getData().get(position).getMedicalRecord());
                bundle.putString("address", myAdapter.getData().get(position).getAddress());
                bundle.putString("eventDate", BaseUtils.getTime(myAdapter.getData().get(position).getEventDate()));
                bundle.putString("eventContent", myAdapter.getData().get(position).getEventContent());
                bundle.putString("pic_01", myAdapter.getData().get(position).getPic1());
                bundle.putString("pic_02", myAdapter.getData().get(position).getPic2());
                bundle.putString("pic_03", myAdapter.getData().get(position).getPic3());
                ActivityUtils.startActivity(bundle, ChildSicknessRecordActivity.class);
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        childId = aCache.getAsString(BaseConstant.childId);
        smart_refresh.autoRefresh();
    }

    @Override
    public void onComplete() {
        smart_refresh.finishRefresh();
        smart_refresh.finishLoadMore();
    }

    @Override
    @OnClick({R.id.title_left})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.child_sickness_record) {
            ActivityUtils.startActivityForResult(this, ChildSicknessRecordActivity.class, 200);
        } else if (id == R.id.child_sickness_count) {
            new XPopup.Builder(context)
                    .maxWidth((int) (ScreenUtils.getScreenWidth() * 0.8))
                    .asCenterList("请选择需要查看的内容", sickness_count, new OnSelectListener() {
                        @Override
                        public void onSelect(int position, String text) {
                            child_sickness_count.setText(text);
                            maxCount = position;
                            smart_refresh.autoRefresh();
                            smart_refresh.setEnableLoadMore(false);
                        }
                    }).show();
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                presenter.getChildSickness(tenantId, token, childId, "medical", pageNum, maxCount);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(ChildSicknessBean childSicknessBean) {
        myAdapter.addData(childSicknessBean.getResult().getItems());
        if (childSicknessBean.getResult().getTotalCount() > myAdapter.getData().size() && maxCount == 0) {
            pageNum++;
            smart_refresh.setEnableLoadMore(true);
        } else {
            smart_refresh.setEnableLoadMore(false);
        }
    }
}